﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Resources;
using System.ComponentModel;
using VIBlend.WPF.Controls;
using System.Reflection;
using System.Data;
using AnonsaiWPF.NwindDataSetTableAdapters;
using System.Windows.Threading;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class RealTimeUpdateDemo : Window, IWindow
    {
        #region IWindow Members

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }
        #endregion

        DispatcherTimer timer = new DispatcherTimer();

        public RealTimeUpdateDemo()
        {
            InitializeComponent();
            this.dataGrid1.SelectionMode = VIBlend.WPF.Controls.DataGrid.SELECTION_MODE.FULL_ROW_SELECT;
            this.dataGrid1.MultipleSelectionEnabled = false;
            this.dataGrid1.RowsHierarchy.Visible = false;

            HierarchyItem column1 = this.dataGrid1.ColumnsHierarchy.Items.Add("Company");
            HierarchyItem column2 = this.dataGrid1.ColumnsHierarchy.Items.Add("Last Sale");
            HierarchyItem column3 = this.dataGrid1.ColumnsHierarchy.Items.Add("Change Net / %");
            HierarchyItem column4 = this.dataGrid1.ColumnsHierarchy.Items.Add("Share Volume");

            column1.Width = 100;
            column2.Width = 133;
            column3.Width = 165;
            column4.Width = 170;

            for(int i = 0; i < 14; i++)
            {
                this.dataGrid1.RowsHierarchy.Items.Add("");
            }

            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[0], this.dataGrid1.ColumnsHierarchy.Items[0], "CTFO");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[1], this.dataGrid1.ColumnsHierarchy.Items[0], "EDAP");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[2], this.dataGrid1.ColumnsHierarchy.Items[0], "CARV");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[3], this.dataGrid1.ColumnsHierarchy.Items[0], "PWRD");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[4], this.dataGrid1.ColumnsHierarchy.Items[0], "INTC");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[5], this.dataGrid1.ColumnsHierarchy.Items[0], "MU");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[6], this.dataGrid1.ColumnsHierarchy.Items[0], "QQQQ");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[7], this.dataGrid1.ColumnsHierarchy.Items[0], "MSFT");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[8], this.dataGrid1.ColumnsHierarchy.Items[0], "SBRO");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[9], this.dataGrid1.ColumnsHierarchy.Items[0], "ANLY");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[10], this.dataGrid1.ColumnsHierarchy.Items[0], "FQEW");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[11], this.dataGrid1.ColumnsHierarchy.Items[0], "TWCC");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[12], this.dataGrid1.ColumnsHierarchy.Items[0], "BMWC");
            this.dataGrid1.CellsArea.SetCellValue(this.dataGrid1.RowsHierarchy.Items[13], this.dataGrid1.ColumnsHierarchy.Items[0], "GINC");

            column1.CellStyle = Application.Current.Resources["TextCenteredCellStyle"] as Style;
            column4.CellStyle = this.Resources["BlueCellStyle"] as Style;
            column2.CellDataSource = GridCellDataSource.Virtual;
            column3.CellDataSource = GridCellDataSource.Virtual;
            column4.CellDataSource = GridCellDataSource.Virtual;

            column3.CellDataTemplate = this.Resources["GreenArrow"] as DataTemplate;
            column2.CellStyle = this.Resources["GreenCellStyle"] as Style;
           
            this.dataGrid1.CellValueNeeded += new VIBlend.WPF.Controls.DataGrid.CellValueNeededEventHandler(Grid1_CellValueNeeded);
       
            this.dataGrid1.Refresh();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = new TimeSpan(0, 0, 0, 0, 400);
            this.timer.Start();

            items.Add(new DataItem() { LastSale = 19.61, ChangeNet = 0.21, ShareVolume = 98952296 });
            items.Add(new DataItem() { LastSale = 21.015, ChangeNet = 0.07, ShareVolume = 41782705 });
            items.Add(new DataItem() { LastSale = 19.61, ChangeNet = 0.21, ShareVolume = 98952296 });
            items.Add(new DataItem() { LastSale = 25.83, ChangeNet = 0.27, ShareVolume = 26360976 });
            items.Add(new DataItem() { LastSale = 52.43, ChangeNet = 0.83, ShareVolume = 551235 });
            items.Add(new DataItem() { LastSale = 16.99, ChangeNet = 0.84, ShareVolume = 231572 });
            items.Add(new DataItem() { LastSale = 14.39, ChangeNet = 0.13, ShareVolume = 100235 });
            items.Add(new DataItem() { LastSale = 6.23, ChangeNet = 0.135, ShareVolume = 89231 });
            items.Add(new DataItem() { LastSale = 19.61, ChangeNet = 0.21, ShareVolume = 266679 });
            items.Add(new DataItem() { LastSale = 47.72, ChangeNet = 0.76, ShareVolume = 7700 });
            items.Add(new DataItem() { LastSale = 308.43, ChangeNet = 7.93, ShareVolume = 1416738 });
            items.Add(new DataItem() { LastSale = 13.665, ChangeNet = 0.31, ShareVolume = 266679 });
            items.Add(new DataItem() { LastSale = 15.13, ChangeNet = 0.9, ShareVolume = 26700 });
            items.Add(new DataItem() { LastSale = 10.62, ChangeNet = 0.51, ShareVolume = 6099 });
            items.Add(new DataItem() { LastSale = 9.21, ChangeNet = 0.6, ShareVolume = 16865263 });

            for (int i = 0; i < items.Count; i++)
            {
                DataItem item = new DataItem();
                item.ChangeNet = items[i].ChangeNet;
                item.LastSale = items[i].LastSale;
                item.ShareVolume = items[i].ShareVolume;
                backupItems.Add(item);
            }
        }

        List<DataItem> items = new List<DataItem>();
        List<DataItem> backupItems = new List<DataItem>();
        public class DataItem
        {
            public double LastSale
            {
                get;
                set;
            }

            public double ChangeNet
            {
                get;
                set;
            }

            public double ShareVolume
            {
                get;
                set;
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            this.dataGrid1.Refresh();
        }

        public class DataObject
        {
            public string LeftValue
            {
                get;
                set;
            }

            public string RightValue
            {
                get;
                set;
            }
        }

       

        Random random = new Random();
        void Grid1_CellValueNeeded(object sender, CellValueNeededEventArgs args)
        {
            int itemIndex = args.RowItem.ItemIndex;

            if (args.ColumnItem.Text == "Last Sale")
            {            
                double calculatedValue = this.items[args.RowItem.ItemIndex].LastSale;
                if (DateTime.Now.Second % 3 != 0)
                {
                    calculatedValue += random.NextDouble();
                }
                else
                {
                    calculatedValue -= 2 * random.NextDouble();
                }

                double newValue = calculatedValue;
                if (newValue < 0)
                {
                    newValue = backupItems[itemIndex].LastSale;
                }

                this.items[args.RowItem.ItemIndex].LastSale = newValue;

                double value = this.items[args.RowItem.ItemIndex].LastSale;
                if (value <= 15)
                {
                    this.dataGrid1.CellsArea.SetCellStyle(args.RowItem, args.ColumnItem, this.Resources["RedCellStyle"] as Style);
                }
                else
                {
                    this.dataGrid1.CellsArea.SetCellStyle(args.RowItem, args.ColumnItem, this.Resources["GreenCellStyle"] as Style);
                }

                args.CellValue = value.ToString("c2");
            }
            else if (args.ColumnItem.Text == "Change Net / %")
            {
                double calculatedValue = this.items[args.RowItem.ItemIndex].ChangeNet;
                if (DateTime.Now.Second % 3 != 0)
                {
                    calculatedValue += random.NextDouble();
                }
                else
                {
                    calculatedValue -= 2 * random.NextDouble();
                }

                this.items[itemIndex].ChangeNet = calculatedValue;
                if (this.items[itemIndex].ChangeNet < 0)
                {
                    this.items[itemIndex].ChangeNet = this.backupItems[itemIndex].ChangeNet;
                }

                double leftValue = this.items[args.RowItem.ItemIndex].ChangeNet;
                double rightValue = leftValue / this.items[args.RowItem.ItemIndex].LastSale;
                rightValue = 100 * rightValue;

                DataObject obj = new DataObject() { LeftValue = leftValue.ToString("f2"), RightValue = rightValue.ToString("f2") + "%" };
                args.CellValue = obj;
                this.dataGrid1.CellsArea.SetCellDataContext(args.RowItem, args.ColumnItem, obj);
      
                if (leftValue >= 3)
                {
                    this.dataGrid1.CellsArea.SetCellDataTemplate(args.RowItem, args.ColumnItem, this.Resources["GreenArrow"] as DataTemplate);
                }
                else
                {
                    this.dataGrid1.CellsArea.SetCellDataTemplate(args.RowItem, args.ColumnItem, this.Resources["RedArrow"] as DataTemplate);                
                }          
            }
            else
            {
                
                double calculatedValue = this.items[args.RowItem.ItemIndex].ShareVolume;
                if (DateTime.Now.Second == 0)
                {
                    calculatedValue += random.Next(0, 100);
                }
                else
                {
                    calculatedValue -= random.Next(0, 100);
                }

                this.items[args.RowItem.ItemIndex].ShareVolume = calculatedValue;
                if (this.items[args.RowItem.ItemIndex].ShareVolume < 0)
                {
                    this.items[args.RowItem.ItemIndex].ShareVolume = 551235;
                }

                args.CellValue = calculatedValue.ToString("n2");
            }            
        }

        public void SetTheme()
        {
            dataGrid1.FontFamily = new FontFamily("Tahoma");
        }
    } 


}
